package team.hyznrj.studentsys.converter;

import jdk.nashorn.internal.ir.IfNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.hyznrj.studentsys.dao.repository.AcademicYearsRepository;
import team.hyznrj.studentsys.dao.repository.CourseAchievementRepository;
import team.hyznrj.studentsys.dao.repository.CurriculumNatureRepository;
import team.hyznrj.studentsys.entity.AcademicYears;
import team.hyznrj.studentsys.entity.CourseAchievement;
import team.hyznrj.studentsys.entity.CurriculumNature;
import team.hyznrj.studentsys.enums.MarkEnum;
import team.hyznrj.studentsys.exception.MarkException;
import team.hyznrj.studentsys.form.CourseAchievementForm;
import team.hyznrj.studentsys.utils.KeyUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ReMidDream
 * @date 2018-03-30 18:57
 **/
@Component
public class CourseAchievementForm2CourseAchievement {

    @Autowired
    private CurriculumNatureRepository curriculumNatureRepository;
    @Autowired
    private AcademicYearsRepository academicYearsRepository;
    
    @Autowired
    private CourseAchievementRepository courseAchievementRepository;

    public CourseAchievementForm2CourseAchievement(CurriculumNatureRepository curriculumNatureRepository, AcademicYearsRepository academicYearsRepository) {
        this.curriculumNatureRepository = curriculumNatureRepository;
        this.academicYearsRepository = academicYearsRepository;
    }

    public  List<CourseAchievement> converter(List<CourseAchievementForm> courseAchievementForms){
        //查询课程性质和学年表
        List<CurriculumNature> curriculumNatureList = curriculumNatureRepository.findAll();
        List<AcademicYears> academicYearsList = academicYearsRepository.findAll();
        Integer curriculumNatureId = null;
        Integer academicYearId = null;
        CourseAchievement courseAchievement = null;
        List<CourseAchievement> courseAchievementList = new ArrayList<>();

        for (CourseAchievementForm courseAchievementForm : courseAchievementForms) {

            // 折算成绩合法性判断
            if (courseAchievementForm.getConvertMark().compareTo(new BigDecimal(0)) < 0) {
                throw new MarkException(MarkEnum.MARK_ERROR);
            }

            //动态拼接
            //判断是否存在于课程性质表
            if (courseAchievementForm.getCurriculumNature() !=null) {
                curriculumNatureId = getCurriculumNatureId(curriculumNatureList,courseAchievementForm);
            }

            //判断是否存在于学年表
            if (courseAchievementForm.getAcademicYear()!=null) {
                academicYearId = getAcademicYearId(academicYearsList,courseAchievementForm);
            }

            //根据是否存在id进行更新或者插入成绩
            if (courseAchievementForm.getId()!=null) {
                /*
                    更新
                 */
                courseAchievement = courseAchievementRepository.findOne(courseAchievementForm.getId());

                if (courseAchievementForm.getStudentId() == null) {
                    courseAchievementForm.setStudentId(courseAchievement.getStudentId());
                }

                if (courseAchievementForm.getCourseTitle()==null) {
                    courseAchievementForm.setCourseTitle(courseAchievement.getCourseTitle());

                }

                if (courseAchievementForm.getCourseCredit()==null) {
                    courseAchievementForm.setCourseCredit(courseAchievement.getCourseCredit());

                }

                if (courseAchievementForm.getRemarks()==null) {
                    courseAchievementForm.setRemarks(courseAchievement.getRemarks());

                }

                if (courseAchievementForm.getConvertMark()==null) {
                    courseAchievementForm.setConvertMark(courseAchievement.getConvertMark());

                }

                if (courseAchievementForm.getCourseGradePoint()==null) {
                    courseAchievementForm.setCourseGradePoint(courseAchievement.getCourseGradePoint());

                }

                if (courseAchievementForm.getSchoolTerm()==null) {
                    courseAchievementForm.setSchoolTerm(courseAchievement.getSchoolTerm());

                }

                if (courseAchievementForm.getUserId()==null) {
                    courseAchievementForm.setUserId(courseAchievement.getUserId());

                }

                if (courseAchievementForm.getAcademicYear()!=null) {
                    courseAchievement.setAcademicYear(getAcademicYearId(academicYearsList,courseAchievementForm));
                }
                if (courseAchievementForm.getCurriculumNature()!=null) {
                    courseAchievement.setCurriculumNature(getCurriculumNatureId(curriculumNatureList,courseAchievementForm));
                }

                courseAchievement =new CourseAchievement(
                        courseAchievementForm.getId(),
                        courseAchievementForm.getStudentId(),
                        courseAchievementForm.getCourseTitle(),
                        courseAchievementForm.getCourseCredit(),
                        courseAchievementForm.getRemarks(),
                        courseAchievementForm.getConvertMark(),
                        courseAchievementForm.getCourseGradePoint(),
                        courseAchievement.getCurriculumNature(),
                        courseAchievement.getAcademicYear(),
                        courseAchievementForm.getSchoolTerm(),
                        1,
                        courseAchievementForm.getUserId());
            }else{

                //判断课程是否已被插入
                if (courseAchievementRepository.findByStudentIdAndCourseTitle(courseAchievementForm.getStudentId(),courseAchievementForm.getCourseTitle()) != null) {
                    courseAchievementRepository.deleteByStudentIdAndCourseTitle(courseAchievementForm.getStudentId(),courseAchievementForm.getCourseTitle());
                }

                /*
                    插入
                 */
                courseAchievement =new CourseAchievement(KeyUtil.getUniqueKey(),courseAchievementForm.getStudentId(),courseAchievementForm.getCourseTitle(),
                        courseAchievementForm.getCourseCredit(),courseAchievementForm.getRemarks(),courseAchievementForm.getConvertMark(),courseAchievementForm.getCourseGradePoint(),
                        curriculumNatureId,academicYearId,courseAchievementForm.getSchoolTerm(),1,courseAchievementForm.getUserId());
            }

            courseAchievementList.add(courseAchievement);
        }

        return courseAchievementList;
    }

    private Integer getCurriculumNatureId(List<CurriculumNature> curriculumNatureList,CourseAchievementForm courseAchievementForm){
        Integer curriculumNatureId =null;
        for (CurriculumNature curriculumNature : curriculumNatureList) {
            if (curriculumNature.getCurriculumNatureName().equals(courseAchievementForm.getCurriculumNature())) {
                curriculumNatureId = curriculumNature.getId();
                break;
            }
        }
        //若课程性质表不存在则添加至课程性质表
        if (curriculumNatureId==null) {
            CurriculumNature curriculumNature = new CurriculumNature(courseAchievementForm.getCurriculumNature());
            CurriculumNature nature = curriculumNatureRepository.save(curriculumNature);
            curriculumNatureId = nature.getId();
        }
        return  curriculumNatureId;
    }

    private Integer getAcademicYearId(List<AcademicYears> academicYearsList,CourseAchievementForm courseAchievementForm) {
        Integer academicYearId = null;
        for (AcademicYears academicYear : academicYearsList) {
            if (academicYear.getAcademicYear().equals(courseAchievementForm.getAcademicYear())) {
                academicYearId = academicYear.getId();
                break;
            }
        }
        //若学年表不存在则添加至学年表
        if (academicYearId == null) {
            AcademicYears academicYears = new AcademicYears(courseAchievementForm.getAcademicYear());
            AcademicYears year = academicYearsRepository.save(academicYears);
            academicYearId = year.getId();
        }
        return academicYearId;
    }

}
